package com.jz.util;
/*
 * @(#)Blacklist.java  2006-8-11
 * 
 * Copyright 2005 Yupoo. All rights reserved.
 
package com.www.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


*//**
 * <p>
 * <a href="Blacklist.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author 5jxiang
 * @version $Id: Blacklist.java 1416 2010-07-19 04:33:06Z 5jxiang $
 *//*
public class Blacklist {
    
    private static Log log = LogFactory.getLog(Blacklist.class);
    
    private static final String VERSION_TIME = "#lastVersionTime:";  //yyyy/MM/dd HH:mm:ss
    private static final String RESERVED_FILE_NAME = "reserved.txt";
    private static final String BLACK_FILE_NAME = "blacklist.txt";
    private static final long CHECK_INTERVAL = 1000 * 60 * 60 * 24 * 2; //2天

    private static Blacklist blacklist;
    
    private Date lastCheckTime = null;
    private Date blackVersionTime = null;
    private Date reservedVersionTime = null;
    private String sourceFilePath;
    private List blacklistStr;
    private List blacklistRegex;
    
    private List reservedlistStr;
    private List reservedlistRegex;
    

    *//**
     * Hide constructor
     *//*
    private Blacklist(String sourceFilePath) {
    	this.sourceFilePath = sourceFilePath;
    	blacklistStr = new LinkedList();
        blacklistRegex = new LinkedList();
        reservedlistStr = new LinkedList();
        blacklistRegex = new LinkedList();
    }
    
    public static void initBlackList(String sourceFilePath) {
    	blacklist = new Blacklist(sourceFilePath);
    	blacklist.loadBlacklistFromFile();
    	blacklist.loadReservedlistFromFile();
    }
    *//**
     * Singleton factory method.
     *//*
    public static Blacklist getBlacklist() {
		if (blacklist.getLastCheckTime() != null && blacklist.getLastCheckTime().getTime() < System.currentTimeMillis() - CHECK_INTERVAL) {
			blacklist.loadBlacklistFromFile();
			blacklist.loadReservedlistFromFile();
			blacklist.setLastCheckTime(new Date());
		}
        return blacklist;
    }
    
    public void init(String sourceFilePath) {
    	this.sourceFilePath = sourceFilePath;
    	
    }
    
    *//**
     * Load the blacklist from the file system.
     *
     * We look for a previously downloaded version of the blacklist first and
     * if it's not found then we load the default blacklist packed with Roller.
     *//*
    private void loadBlacklistFromFile() {
    	if (sourceFilePath == null) return;
        InputStream txtStream = null;
        try {
            File blacklistFile = new File(sourceFilePath + BLACK_FILE_NAME);
            
            if (!blacklistFile.exists()) {
            	return ;
            }
            
            txtStream = new FileInputStream(blacklistFile);
            
            if (log.isDebugEnabled()) {
            	log.debug("Loading blacklist from " + sourceFilePath + BLACK_FILE_NAME);
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
            	log.debug("Couldn't find downloaded blacklist, " +
            				"loading from classpath instead");
            }
        }
        
        if (txtStream != null) {
        	List newStrList = new LinkedList();
        	List newRegxList = new LinkedList();
        	this.blackVersionTime = readFromStream(txtStream, newStrList, newRegxList, this.blackVersionTime);
            if (newStrList.size() > 0) {
            	 synchronized (this.blacklistStr){
            		 this.blacklistStr.clear();
            		 this.blacklistStr = newStrList;
            	 }
            }
            if (newRegxList.size() > 0) {
	           	 synchronized (this.blacklistRegex){
	           		 this.blacklistRegex.clear();
	           		 this.blacklistRegex = newRegxList;
	           	 }
           }
        } else {
            log.error("couldn't load a blacklist file from anywhere, "+
                    "this means blacklist checking is disabled for now.");
        }
    }
    
    private void loadReservedlistFromFile() {
    	if (sourceFilePath == null) return;
        InputStream txtStream = null;
        try {
            File blacklistFile = new File(sourceFilePath + RESERVED_FILE_NAME);
            
            if (!blacklistFile.exists()) {
            	return ;
            }
            
            txtStream = new FileInputStream(blacklistFile);
            
            if (log.isDebugEnabled()) {
            	log.debug("Loading blacklist from " + sourceFilePath + RESERVED_FILE_NAME);
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
            	log.debug("Couldn't find downloaded blacklist, " +
            				"loading from classpath instead");
            }
        }
        
        if (txtStream != null) {
        	List newStrList = new LinkedList();
        	List newRegxList = new LinkedList();
        	this.reservedVersionTime = readFromStream(txtStream, newStrList, newRegxList, this.reservedVersionTime);
            if (newStrList.size() > 0) {
            	 synchronized (this.reservedlistStr){
            		 this.reservedlistStr.clear();
            		 this.reservedlistStr = newStrList;
            	 }
            }
            if (newRegxList.size() > 0) {
	           	 synchronized (this.reservedlistRegex){
	           		 this.reservedlistRegex.clear();
	           		 this.reservedlistRegex = newRegxList;
	           	 }
           }
        } else {
            log.error("couldn't load a blacklist file from anywhere, "+
                    "this means blacklist checking is disabled for now.");
        }
    }
    
    *//**
     * Read in the InputStream for rules.
     * @param txtStream
     *//*
    private Date readFromStream(InputStream txtStream, List strList, List rexList, Date lastVersionTime) {
        String line;
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader( txtStream, "UTF-8" ) );
            synchronized (this) {
	            while ((line = in.readLine()) != null) {
	                if (line.startsWith(VERSION_TIME)) {
	                   Date date = readLastVersionTime(line);
	                   if (date == null || (lastVersionTime != null && !date.after(lastVersionTime))) {
	                	   break;
	                   } else {
	                	   lastVersionTime = date;
	                   }
	                } else if (!line.startsWith("#")){
	                    readRule(line, strList, rexList);
	                } 
	            }
            }
        } catch (Exception e) {
            log.error(e);
        } finally {
            try {
                if (in != null) in.close();
            } catch (IOException e1) {
                log.error(e1);
            }
            return lastVersionTime;
        }
       
    }
    
    
    *//**
     * @param str
     *//*
    private void readRule(String str, List strList, List regexList) {
        if (str == null) return;
        str = str.toLowerCase().trim();
    	if (org.apache.commons.lang.StringUtils.isEmpty(str)) return; // bad condition
    	 String rule = str;
        if (str.indexOf("#") > 0) // line has a comment
        {
            int commentLoc = str.indexOf("#");
            rule = str.substring(0, commentLoc-1).trim(); // strip comment
        }
        
        if (rule.indexOf( "(" ) > -1) // regex rule
        {
            // pre-compile patterns since they will be frequently used
        	regexList.add(Pattern.compile(rule));
        } else if (org.apache.commons.lang.StringUtils.isNotEmpty(rule)) {
        	strList.add(rule);
        }
    }
    
    
    *//**
     * Try to parse out "Last update" value: 2004/03/08 23:17:30.
     * @param str
     *//*
    private Date readLastVersionTime(String str) {
        int lastUpdatePos = str.indexOf(VERSION_TIME);
        if (lastUpdatePos > -1) {
            str = str.substring(lastUpdatePos + VERSION_TIME.length());
            str = str.trim();
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                return sdf.parse(str);
            } catch (ParseException e) {
            	if (log.isDebugEnabled()) {
            		log.debug("ParseException reading " + str);
            	}
            }
        }
        return null;
    }
    
    
    *//**
     * Does the String argument match any of the rules in the blacklist?
     *
     * @param str
     * @return
     *//*
    public boolean isBlacklisted(String str) {
        if (str == null || org.apache.commons.lang.StringUtils.isEmpty(str)) return false;
        if( testStringRules(str.toLowerCase(), this.blacklistStr) ) return true;
        return testRegExRules(str.toLowerCase(), this.blacklistRegex);
    }
    
    public boolean equalReserved(String str) {
        if (str == null || org.apache.commons.lang.StringUtils.isEmpty(str)) return false;
        if( equalStringRules(str.toLowerCase(), this.reservedlistStr) ) return true;
        return false;
    }
    
	*//**
     * Test String against the RegularExpression rules.
     *
     * @param str
     * @return
     *//*
    private boolean testRegExRules(String str, List regList) {
        Pattern testPattern = null;
        Iterator iter = regList.iterator();
        while (iter.hasNext()) {
            testPattern = (Pattern)iter.next();
            if (testPattern.matcher(str).find()) {
               return true;
            }
        }
        return false;
    }
    
    
    *//**
     * Test the String against the String rules,
     * using simple indexOf.
     *
     * @param str
     * @return
     *//*
    private boolean testStringRules(String str, List strList) {
        String test;
        Iterator iter = strList.iterator();
        while (iter.hasNext()) {
            test = (String)iter.next();
            if (str.indexOf(test) > -1) {
                return true;
            }
        }
        return false;
    }
    
    private boolean equalStringRules(String str, List strList) {
        String test;
        Iterator iter = strList.iterator();
        while (iter.hasNext()) {
            test = (String)iter.next();
            if (test.equals(str)) {
               return true;
            }
        }
        return false;
    }
    
    
    *//**
     * Return pretty list of String and RegEx rules.
     *//*
    public String toString() {
        return "";
    }

	*//**
	 * @return the blacklistRegex
	 *//*
	public List getBlacklistRegex() {
		return blacklistRegex;
	}

	*//**
	 * @param blacklistRegex the blacklistRegex to set
	 *//*
	public void setBlacklistRegex(List blacklistRegex) {
		this.blacklistRegex = blacklistRegex;
	}

	*//**
	 * @return the blacklistStr
	 *//*
	public List getBlacklistStr() {
		return blacklistStr;
	}

	*//**
	 * @param blacklistStr the blacklistStr to set
	 *//*
	public void setBlacklistStr(List blacklistStr) {
		this.blacklistStr = blacklistStr;
	}

	*//**
	 * @return the sourceFilePath
	 *//*
	public String getSourceFilePath() {
		return sourceFilePath;
	}

	*//**
	 * @param sourceFilePath the sourceFilePath to set
	 *//*
	public void setSourceFilePath(String sourceFilePath) {
		this.sourceFilePath = sourceFilePath;
	}

	*//**
	 * @return the lastCheckTime
	 *//*
	public Date getLastCheckTime() {
		return lastCheckTime;
	}

	*//**
	 * @param lastCheckTime the lastCheckTime to set
	 *//*
	public void setLastCheckTime(Date lastCheckTime) {
		this.lastCheckTime = lastCheckTime;
	}


}
*/